package com.wan.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wan.service.BatchingInfoService;
import com.wan.service.MenuInfoService;
import com.wan.service.VegetableBatchService;
import com.wan.service.VegetableInfoService;
import com.wan.service.system.SysUserervice;
import entity.common.BatchingInfo;
import entity.common.MenuInfo;
import entity.common.VegetableBatch;
import entity.common.VegetableInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.Map;
import java.util.Objects;

import static java.util.stream.Collectors.toList;

/**
 * 后台管理 home控制层
 * @author xzx
 */
@Slf4j
@Controller
@RequestMapping("/caipu")
public class HomeController {
    @Autowired
    private BatchingInfoService batchingInfoService;
    @Autowired
    private MenuInfoService menuInfoService;
    @Autowired
    private SysUserervice userervice;
    @Autowired
    private VegetableInfoService vegetableInfoService;
    @Autowired
    private VegetableBatchService vegetableBatchService;

    @RequestMapping("/test")
    public String tsetSession(Model model) {
        model.addAttribute("sessionTimeout","登录超时,请重新登录");
        return "redirect:/user/login";
    }

    /**
     * 跳转首页
     * @return
     */
    @RequestMapping("/index")
    public ModelAndView gotoIndex(){
        ModelAndView modelAndView = new ModelAndView("index");
        return modelAndView;
    }

    @RequestMapping("/upload/food")
    public ModelAndView gotoUploadTable(){
        ModelAndView modelAndView = new ModelAndView("uploadfood");
        //todo 查询配料列表 list
        List<BatchingInfo> batchingInfos = batchingInfoService.list();
        List<MenuInfo> menuInfos = menuInfoService.list();
        modelAndView.addObject("batchingInfos",batchingInfos);
        modelAndView.addObject("menuInfos",menuInfos);
        return modelAndView;
    }

    @RequestMapping("/edit/food/page/{vegetableId}")
    public ModelAndView gotoEditTable(@PathVariable("vegetableId") String vegetableId){
        ModelAndView modelAndView = new ModelAndView("editfood");
        VegetableInfo vegetableInfo = vegetableInfoService.getById(vegetableId);
        //todo 查询配料列表 list
        List<BatchingInfo> batchingInfos = batchingInfoService.list();
        List<MenuInfo> menuInfos = menuInfoService.list();
        QueryWrapper<VegetableBatch> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("vegetable_id",vegetableId);
        List<VegetableBatch> vegetableBatcheslist = vegetableBatchService.list(queryWrapper);

        for (BatchingInfo batchingInfo : batchingInfos) {
            for (VegetableBatch vegetableBatch : vegetableBatcheslist) {
                if (batchingInfo.getId().equals(vegetableBatch.getBatchingId())) {
                    batchingInfo.setFlag(true);
                    continue;
                }
            }
        }
        modelAndView.addObject("batchingInfos",batchingInfos);
        modelAndView.addObject("menuInfos",menuInfos);
        modelAndView.addObject("obj",vegetableInfo);
        return modelAndView;
    }


    public static List<Map<Object, Object>> compareListHitData(List<Map<Object, Object>> oneList, List<Map<Object, Object>> twoList) {
        List<Map<Object, Object>> resultList = oneList.stream().map(map -> twoList.stream()
                .filter(m -> Objects.equals(m.get("id"), map.get("id")))
                .findFirst().map(m -> {
                    map.putAll(m);
                    return map;
                }).orElse(null))
                .filter(Objects::nonNull).collect(toList());
        return resultList;
    }


    @RequestMapping("/vegetable/list")
    public ModelAndView getVegetable() {
        Integer pageSize = 10;
        ModelAndView modelAndView = new ModelAndView("vegetablelist");
        IPage<VegetableInfo> page = new Page<>(1,pageSize);
        QueryWrapper<VegetableInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("update_time");
        IPage<VegetableInfo> vegetableInfoIPage = vegetableInfoService.page(page,queryWrapper);
        Long count = vegetableInfoIPage.getTotal();
        long totalPage  = (count + pageSize -1) / pageSize;
        List<VegetableInfo> vegetableInfoList = vegetableInfoIPage.getRecords();
        List<MenuInfo> menuInfos = menuInfoService.list();
        modelAndView.addObject("meuList",menuInfos);
        modelAndView.addObject("list",vegetableInfoList);
        modelAndView.addObject("count",count);
        modelAndView.addObject("totalPage",totalPage);
        return modelAndView;
    }

    @RequestMapping("/batching/list")
    public ModelAndView getBatching() {
        ModelAndView modelAndView = new ModelAndView("batchlist");
        IPage<BatchingInfo> page = new Page<>(1,10);
        IPage<BatchingInfo> batchingInfoIPage = batchingInfoService.page(page);
        Long count = batchingInfoIPage.getTotal();
        List<BatchingInfo> batchingInfoList = batchingInfoIPage.getRecords();
        modelAndView.addObject("list",batchingInfoList);
        modelAndView.addObject("count",count);
        return modelAndView;
    }


    @RequestMapping("/upload/batch")
    public ModelAndView gotoUploadBatch(){
        ModelAndView modelAndView = new ModelAndView("uploadbatch");
        return modelAndView;
    }

    @RequestMapping("/main")
    public ModelAndView gotoMain() {
        ModelAndView modelAndView = new ModelAndView("main");
        return modelAndView;
    }

    @RequestMapping("/personInfo")
    public ModelAndView gotoPersonInfo() {
        ModelAndView modelAndView = new ModelAndView("personInfo");
        return modelAndView;
    }

    @RequestMapping("/changepwd")
    public ModelAndView gotoChangePassWord() {
        ModelAndView modelAndView = new ModelAndView("changepwd");
        return modelAndView;
    }

    @RequestMapping("/myloginfo")
    public ModelAndView gotoMyloginfo(){
        ModelAndView modelAndView = new ModelAndView("myloginfo");
        return modelAndView;
    }



}
